<template>
  <div class="app-container" style="padding-top: 5px">
    <el-tabs v-model="queryParams.templateType" @tab-click="tagsChange">
      <el-tab-pane v-if="showRegion" label="省区模板" name="1"></el-tab-pane>
      <el-tab-pane v-if="showInfirmary" label="院内模板" name="2"></el-tab-pane>
      <el-tab-pane v-if="showPersonage" label="个人模板" name="3"></el-tab-pane>
    </el-tabs>

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键字" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入模板名称/主治/功效搜索"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 230px"
        />
      </el-form-item>
      <el-form-item v-if="queryParams.templateType === '1'" label="省份筛选" prop="regionCode">
        <el-select
          v-model="queryParams.regionCode"
          placeholder="请选择省份"
          clearable
          filterable
          @change="handleQuery"
        >
          <el-option
            v-for="item in areaList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模版归属" prop="ownerId">
        <Treeselect
          v-if="queryParams.templateType === '1'"
          ref="Treeselect"
          v-model="queryParams.ownerId"
          :options="channelOptions"
          :show-count="true"
          placeholder="请选择模板归属渠道"
          noResultsText="暂无数据"
          style="width: 300px"
          @input="handleQuery"
        />
        <!-- 院内选择单位 -->
        <SelectCompany
          v-if="queryParams.templateType === '2'"
          :defaultValue.sync="queryParams.ownerId"
          :companyType="0"
          placeholder="请选择模板归属单位"
          style="width: 100%"
          @change="handleQuery"
        />
        <!-- 个人选择专家 -->
        <SelectExpert
          v-if="queryParams.templateType === '3'"
          ref="SelectExpert"
          :defaultValue.sync="queryParams.ownerId"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['template:application:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="模板名称" align="center" prop="templateName" />
      <el-table-column label="模板可见范围" align="center" prop="regionName">
        <template slot-scope="{ row }">
          {{ row.templateType == 1 ? row.regionName : (row.templateType == 2 ? '院内可见' : '个人可见') }}
        </template>
      </el-table-column>
      <el-table-column label="模板归属" align="center" prop="ownerName" />
      <el-table-column label="功效" align="center" prop="effect" />
      <el-table-column label="主治" align="center" prop="mainTreat" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button v-hasPermi="['template:application:edit']" size="mini" type="text" @click="handleOpen(scope.row)"
            >编辑</el-button
          >
          <el-button v-hasPermi="['template:application:remove']" size="mini" type="text" @click="handleDelete(scope.row.templateId)"
            >删除</el-button
          >
          <el-button v-hasPermi="['template:application:add']" size="mini" type="text" @click="copyList(scope.row)"
            >复制创建</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <EditTemplate
      ref="EditTemplate"
      :templateCategory="queryParams.templateCategory"
      :templateType="queryParams.templateType"
      :areaList="areaList"
      @refresh="getList"
    />
  </div>
</template>

<script>
import { getTemplateList, deleteTemplate } from "@/api/template/index.js";
import EditTemplate from '../components/edit'
import { listArea } from "@/api/saas/area";
import { hasPermi } from "@/plugins/auth";
import { treeselect } from "@/api/channel/channel";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import SelectCompany from '@/components/SelectCompany/index'
import SelectExpert from '@/views/expert/expert/selectExpert'

export default {
  name: "Application",
  components: {
    EditTemplate,
    Treeselect,
    SelectCompany,
    SelectExpert,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        templateCategory: 1, // 模板类别: 0中药处方模版 1贴敷处方模版
        templateType: '1', // 模板类型: 1省区 2院内 3个人
        regionCode: undefined, // 地区码
        ownerId: undefined
      },
      // 总条数
      total: 0,
      dataList: [],
      areaList: [], // 省份列表
      channelOptions: [], // 渠道树选项
    }
  },
  computed: {
    showPersonage() {
      return hasPermi("template:application:personage")
    },
    showInfirmary() {
      return hasPermi("template:application:infirmary")
    },
    showRegion() {
      return hasPermi("template:application:region")
    }
  },
  created() {
    this.queryParams.templateType = this.showPersonage ? '3' : this.queryParams.templateType;
    this.queryParams.templateType = this.showInfirmary ? '2' : this.queryParams.templateType;
    this.queryParams.templateType = this.showRegion ? '1' : this.queryParams.templateType;
    if(!this.queryParams.templateType) return

    this.getList();
    this.getAreaList();
    this.getTreeselect();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getTemplateList(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 获取省份列表
    getAreaList() {
      listArea({ level: 1 }).then(res => {
        this.areaList = res.data;
      })
    },
    /** 查询渠道下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.channelOptions = response.data;
      });
    },
    tagsChange(tab, event) {
      this.queryParams.templateType = tab.name;
      this.queryParams.regionCode = undefined;
      this.queryParams.ownerId = undefined;
      this.handleQuery()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      const { templateType } = this.queryParams;
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.queryParams.templateType = templateType;
      if(this.$refs.SelectExpert){
        this.$refs.SelectExpert.remoteMethod('');
      }
      this.getList();
    },
    handleOpen(row) {
      this.$refs.EditTemplate.openModal(row);
    },
    copyList(row){
      this.$refs.EditTemplate.openModal(row, true);
    },
    /** 删除按钮操作 */
    handleDelete(templateId) {
      this.$modal
        .confirm("是否确认删除该模板吗？")
        .then(function () {
          return deleteTemplate({templateId});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

  }
}
</script>

<style lang="scss" scoped>
</style>
